package cn.iocoder.yudao.module.product.controller.admin.customermessage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.product.controller.admin.customermessage.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.customermessage.CustomerMessageDO;
import cn.iocoder.yudao.module.product.service.customermessage.CustomerMessageService;

@Tag(name = "管理后台 - 客服消息")
@RestController
@RequestMapping("/product/customer-message")
@Validated
public class CustomerMessageController {

    @Resource
    private CustomerMessageService customerMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建客服消息")
    @PreAuthorize("@ss.hasPermission('product:customer-message:create')")
    public CommonResult<Long> createCustomerMessage(@Valid @RequestBody CustomerMessageSaveReqVO createReqVO) {
        return success(customerMessageService.createCustomerMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服消息")
    @PreAuthorize("@ss.hasPermission('product:customer-message:update')")
    public CommonResult<Boolean> updateCustomerMessage(@Valid @RequestBody CustomerMessageSaveReqVO updateReqVO) {
        customerMessageService.updateCustomerMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:customer-message:delete')")
    public CommonResult<Boolean> deleteCustomerMessage(@RequestParam("id") Long id) {
        customerMessageService.deleteCustomerMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:customer-message:query')")
    public CommonResult<CustomerMessageRespVO> getCustomerMessage(@RequestParam("id") Long id) {
        CustomerMessageDO customerMessage = customerMessageService.getCustomerMessage(id);
        return success(BeanUtils.toBean(customerMessage, CustomerMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服消息分页")
    @PreAuthorize("@ss.hasPermission('product:customer-message:query')")
    public CommonResult<PageResult<CustomerMessageRespVO>> getCustomerMessagePage(@Valid CustomerMessagePageReqVO pageReqVO) {
        PageResult<CustomerMessageDO> pageResult = customerMessageService.getCustomerMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服消息 Excel")
    @PreAuthorize("@ss.hasPermission('product:customer-message:export')")
    public void exportCustomerMessageExcel(@Valid CustomerMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerMessageDO> list = customerMessageService.getCustomerMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服消息.xls", "数据", CustomerMessageRespVO.class,
                        BeanUtils.toBean(list, CustomerMessageRespVO.class));
    }

}